<template>
	<Card shadow>
	    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
	    	<Row>
	    		 <Col :xs="24" :sm="18" :md="12" :lg="12">
	    		 	<FormItem label="真实姓名" prop="name">
			            <Input v-model="formValidate.name" placeholder="真实姓名"></Input>
			        </FormItem>
	    		 </Col>
	    	</Row>
	    	<Row>
		        <Col :xs="24" :sm="18" :md="12" :lg="12">
			        <FormItem label="身份证号码" prop="idcard">
			            <Input v-model="formValidate.idcard" placeholder="身份证号码" @on-blur="gainMessage"></Input>
			        </FormItem>
		        </Col>
		    </Row>
		    <Row>
		        <Col :xs="24" :sm="18" :md="12" :lg="12">
			        <FormItem label="生日" prop="birthday">
			            <Input v-model="formValidate.birthday" placeholder="生日"></Input>
			        </FormItem>
		        </Col>
		    </Row>
		    <Row>
		        <Col :xs="24" :sm="18" :md="12" :lg="12">
			        <FormItem label="年龄" prop="age">
			            <Input v-model="formValidate.age" placeholder="年龄"></Input>
			        </FormItem>
		        </Col>
		    </Row>
	        <FormItem label="性别" prop="sex">
	            <RadioGroup v-model="formValidate.sex">
	                <Radio label="1">男</Radio>
	                <Radio label="0">女</Radio>
	            </RadioGroup>
	        </FormItem>
	        <Row>
		        <Col :xs="24" :sm="18" :md="12" :lg="12">
			        <FormItem label="城市" prop="city">
			            <Input v-model="formValidate.city" placeholder="城市"></Input>
			        </FormItem>
		        </Col>
		    </Row>
		    <Row>
		        <Col :xs="24" :sm="18" :md="12" :lg="12">
			        <FormItem label="密码" prop="password">
			            <Input v-model="formValidate.password" placeholder="密码"></Input>
			        </FormItem>
		        </Col>
		    </Row>
		    <Row>
		        <Col :xs="24" :sm="18" :md="12" :lg="12">
			        <FormItem label="电话" prop="phone">
			            <Input v-model="formValidate.phone" placeholder="电话"></Input>
			        </FormItem>
		        </Col>
		    </Row>
		    <FormItem label="是否启用" prop="status">
	            <RadioGroup v-model="formValidate.status">
	                <Radio label="1">启用</Radio>
	                <Radio label="0">禁用</Radio>
	            </RadioGroup>
	        </FormItem>
	        <FormItem>
	            <Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
	            <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">取消</Button>
	        </FormItem>
	    
	    </Form>
    </Card>
</template>
<script>
    export default {
        data () {
            return {
                formValidate: {
                    name: '',
                    idcard: '',
                    birthday:'',
                    age:'',
                    sex: '',
                    city:'',
                    password:'',
                    phone: '',
                    status:'1'
                },
                ruleValidate: {
                    name: [
                        { required: true, pattern:/^[\u4E00-\u9FA5A-Za-z]+$/, message: '请输入真实姓名', trigger: 'blur' }
                    ],
                    idcard: [
                        { required: true, pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确身份证号码', trigger: 'blur' },
                    ],
                    birthday: [
                        { required: true, pattern: /^(19|20)\d{2}-(1[0-2]|0?[1-9])-(0?[1-9]|[1-2][0-9]|3[0-1])$/, message: "请填写正确生日日期，年月日中间用'-'连接", trigger: 'blur'},
                    ],
                    age: [
                        { required: true, pattern:/^([0-9]|[0-9]{2}|120)$/, message: "请输入正确年龄", trigger: 'blur'},
                    ],
                    sex: [
                        { required: true, message: '请选择性别', trigger: 'change' }
                    ],
                    city: [
                        { required: true, pattern:/^[\u4e00-\u9fa5]+$/, message: '请填写正确城市', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, pattern:/^(\w){6,16}$/, message: '请输入密码，密码必须由6-16个英文字母和数字组成', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, pattern:/^1(3|4|5|7|8)\d{9}$/,message: "请输入正确的电话号码",trigger: 'blur'},
                    ],
                    status: [
                        { required: true, message: "请选择启用状态", trigger: 'change' }
                    ]
                }
            }
        },
        methods: {
//      	保存
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        var that=this;
                        this.axios({
                        	method: 'get',
                        	url: that.$store.state.app.globalUrl+'/admin/Examinee/isExist?idcard='+that.formValidate.idcard,
                        	data:{}
                        }).then(function(res){
                        	if(res.data.data.examinee==null){
                        		that.axios({
						            method: 'post',
						            url: that.$store.state.app.globalUrl+'/admin/Examinee/add',
						            data: {
						            	name:that.formValidate.name,
						            	idcard:that.formValidate.idcard,
						            	birthday:that.formValidate.birthday,
						            	age:that.formValidate.age,
						            	sex:that.formValidate.sex,
						            	city:that.formValidate.city,
						            	password:that.formValidate.password,
						            	phone:that.formValidate.phone,
						            	status:that.formValidate.status,
						            	Id:that.formValidate.id
						            }
						        }).then(function(res){
						        	that.$router.push({
						    			name: 'user-management'
						    		})
						    	})
                        	}else{
                        		that.$Message.error('用户已存在!');
                        	}
                        })
                    } else {
                        this.$Message.error('请把表单填写完整!');
                    }
                })
            },
//          取消
            handleReset (name) {
                this.$router.push({
	    			name: 'user-management'
	    		})
            },
//          根据身份证获取其他信息
            gainMessage(){
            	//身份证地区
		        var areaID = {
		            11: "北京",
		            12: "天津",
		            13: "河北",
		            14: "山西",
		            15: "内蒙古",
		            21: "辽宁",
		            22: "吉林",
		            23: "黑龙江",
		            31: "上海",
		            32: "江苏",
		            33: "浙江",
		            34: "安徽",
		            35: "福建",
		            36: "江西",
		            37: "山东",
		            41: "河南",
		            42: "湖北",
		            43: "湖南",
		            44: "广东",
		            45: "广西",
		            46: "海南",
		            50: "重庆",
		            51: "四川",
		            52: "贵州",
		            53: "云南",
		            54: "西藏",
		            61: "陕西",
		            62: "甘肃",
		            63: "青海",
		            64: "宁夏",
		            65: "新疆",
		            71: "台湾",
		            81: "香港",
		            82: "澳门",
		            91: "国外"
		        };
		        //性别
	            if(this.formValidate.idcard.length == 15) {
	                this.formValidate.sex=this.formValidate.idcard.substring(14, 15) % 2+'';
	            } else if(this.formValidate.idcard.length == 18) {
	                this.formValidate.sex=this.formValidate.idcard.substring(14, 17) % 2+'';
	            } else {
	                //不是15或者18,null
	                this.formValidate.sex='';
	            };
				//生日
	            if(15 == this.formValidate.idcard.length) {
	                this.formValidate.birthday = this.formValidate.idcard.charAt(6) + this.formValidate.idcard.charAt(7);
	                if(parseInt(this.formValidate.birthday) < 10) {
	                    this.formValidate.birthday = '20' + this.formValidate.birthday;
	                } else {
	                    this.formValidate.birthday = '19' + this.formValidate.birthday;
	                }
	                this.formValidate.birthday = this.formValidate.birthday + '-' + this.formValidate.idcard.charAt(8) + this.formValidate.idcard.charAt(9) + '-' + this.formValidate.idcard.charAt(10) + this.formValidate.idcard.charAt(11);
	            } else if(18 == this.formValidate.idcard.length) {
	                this.formValidate.birthday = this.formValidate.idcard.charAt(6) + this.formValidate.idcard.charAt(7) + this.formValidate.idcard.charAt(8) + this.formValidate.idcard.charAt(9) + '-' + this.formValidate.idcard.charAt(10) + this.formValidate.idcard.charAt(11) + '-' + this.formValidate.idcard.charAt(12) + this.formValidate.idcard.charAt(13);
	            };
				//获取出生地
		        this.formValidate.city=areaID[parseInt(this.formValidate.idcard.substr(0, 2))];
		        
				//获取年龄
	            var r = this.formValidate.birthday.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
	            if(r == null) this.formValidate.age='';
	            var d = new Date(r[1], r[3] - 1, r[4]);
	            if(d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
	                var Y = new Date().getFullYear();
	                this.formValidate.age=(Y - r[1]);
	            } else {
	                this.formValidate.age='';
	            };
				//获取身份证密码后6位
				if(15 == this.formValidate.idcard.length) {
	                this.formValidate.password = this.formValidate.idcard.charAt(9) + this.formValidate.idcard.charAt(10) + this.formValidate.idcard.charAt(11) + this.formValidate.idcard.charAt(12) + this.formValidate.idcard.charAt(13) + this.formValidate.idcard.charAt(14);
	            } else if(18 == this.formValidate.idcard.length) {
	                this.formValidate.password = this.formValidate.idcard.charAt(12) + this.formValidate.idcard.charAt(13) + this.formValidate.idcard.charAt(14) + this.formValidate.idcard.charAt(15) + this.formValidate.idcard.charAt(16) + this.formValidate.idcard.charAt(17).toUpperCase();
	            };
		    }
		}      
    }
</script>
